package com.spikything.video {
	
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Rectangle;
	import flash.system.*;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.AntiAliasType;
	import flash.ui.Mouse;
	
	import com.irisnation.ing.formula1.Globals;
	import com.spikything.gfx.*;
	import com.spikything.ui.*;
	import com.spikything.utils.*;
	import com.spikything.video.events.VideoEvent;
	
	import com.irisnation.ing.formula1.Style;
	
	public class VideoControls extends MovieClip {
		
		//protected const SLIDE_DAMPING 		:Number 		= 5;
		protected const TITLE_COLOUR 		:Number 		= 0x655f5f;
		
		//[Embed(source='../../../../assets/gfx/VideoControlsBackground.png')]
		//protected var backgroundBitmapClass :Class;
		protected var background 			:RoundFrame;// :Scale9Bitmap;
		
		protected var btnFullScreen 		:IconButton;// StateButton;
		protected var btnPlay 				:StateButton;
		protected var btnSoundOn 			:StateButton;
		protected var vidScrubber 			:VideoScrubber;
		protected var timecode 				:TextLabel;
		protected var maskRect 				:Sprite;
		
		//protected var slide 				:Number 		= 0;
		protected var videoRect 			:Rectangle;
		protected var disabled 				:Boolean 		= false;
		protected var beacon 				:EventDispatcher;
		protected var fixedX:Number;
		protected var fixedY:Number;
		protected var posFixed:Boolean = false;
		
		private const WIDTH:Number = 386;
		private const HEIGHT:Number = 28;
		
		public var vamoosing 				:Boolean 		= false;
		public var align 					:String;
		public var duration 				:int;
		
		public function VideoControls (align:String, beacon:EventDispatcher, duration:int) {
			
			this.align = align;
			this.beacon = beacon;
			this.duration = duration;
			//this.blendMode = BlendMode.LAYER;
			
			background = new RoundFrame(WIDTH, HEIGHT, 0x000000, .1);// new Scale9Bitmap((new backgroundBitmapClass() as Bitmap).bitmapData, new Rectangle(4, 4, 511, 35));
			addChild(background);
			
			btnPlay = new StateButton("playIcon|pauseIcon");
			btnPlay.addEventListener(MouseEvent.CLICK, playPauseBtnClick);
			addChild(btnPlay);
			
			vidScrubber = new VideoScrubber(beacon, 100, 10, 275);
			addChild(vidScrubber);
			
			timecode = new TextLabel("", new TextFormat("_sans", 9)); //DropshadowedText
			timecode.autoSize = TextFieldAutoSize.NONE;
			timecode.width = 50;
			timecode.height = 15;
			addChild(timecode);
			
			btnSoundOn = new StateButton("soundOffIcon|soundOnIcon");
			btnSoundOn.addEventListener(MouseEvent.CLICK, muteBtnClick);
			btnSoundOn.addEventListener(MouseEvent.ROLL_OVER, openVolumeSlider);
			addChild(btnSoundOn);
			btnSoundOn.selected = true;
			
			btnFullScreen = new IconButton("fullscreenIcon");// StateButton("fullscreenIcon|restoreIcon");
			btnFullScreen.addEventListener(MouseEvent.CLICK, fullscreenClick);
			addChild(btnFullScreen);
			
			maskRect = new Sprite();
			addChild(maskRect);
			mask = maskRect;
			
			beacon.addEventListener(VideoEvent.INFO_PLAYING, onVideoPlaying);
			beacon.addEventListener(VideoEvent.INFO_STOPPED, onVideoStopped);
			beacon.addEventListener(VideoEvent.INFO_MUTED, onVideoMuted);
			beacon.addEventListener(VideoEvent.INFO_UNMUTED, onVideoUnmuted);
			beacon.addEventListener(VideoEvent.INFO_POSITION, onInfoPosition);
			beacon.addEventListener(VideoEvent.INFO_VIDMOVED, onVidMoved);
			addEventListener(Event.ADDED_TO_STAGE, onAdded);
			
		}
		
		public function onAdded (e:Event):void {
			//slide = 0;//background.height;
			show();
		}
		
		public function onVidMoved (ve:VideoEvent):void {
			videoRect = ve.locationRect;
			redrawElements();
		}
		
		public function fixPos (px:Number, py:Number):void {
			x = fixedX = px;
			y = fixedY = py;
			posFixed = true;
		}
		
		protected function redrawElements ():void {
			if (!posFixed) {
				//videoRect.width = 386;
				//videoRect.height = 217;
				x = videoRect.x;
				y = videoRect.height + videoRect.y;
				background.width = videoRect.width;
			}
			//background.y = -slide;
			
			maskRect.graphics.clear();
			maskRect.graphics.beginFill(0, .2);
			maskRect.graphics.drawRect(0, 0, background.width, background.height);
			maskRect.graphics.drawRect(0, -videoRect.height-background.height, videoRect.width, background.height);
			
			var staticCoord:Number = 9;// - slide;
			Utils.layout(StageAlign.LEFT, staticCoord, 5, 5, [btnPlay,vidScrubber,timecode,btnSoundOn,btnFullScreen]);
			timecode.y -= 3;
			
		}
		
		public function vamoose ():void { //sharpish:Boolean=false
			disabled = true;
			vamoosing = true;
			/*
			removeEventListener(Event.ENTER_FRAME, slideIn);
			if (sharpish) {
				removeEventListener(Event.ENTER_FRAME, slideOut);
				Utils.removeChild(parent, this);
			} else {
				addEventListener(Event.ENTER_FRAME, slideOut);
			}
			*/
			Utils.removeChild(parent, this);
		}
		
		public function hide ():void {
			disabled = true;
			visible = false;
			/*
			removeEventListener(Event.ENTER_FRAME, slideIn);
			addEventListener(Event.ENTER_FRAME, slideOut);
			Mouse.hide();
			*/
		}
		
		public function show ():void {
			//if (vamoosing) return;
			disabled = false;
			visible = true;
			/*
			removeEventListener(Event.ENTER_FRAME, slideOut);
			addEventListener(Event.ENTER_FRAME, slideIn);
			Mouse.show();
			*/
		}
		
		/*
		public function slideOut (e:Event):void {
			//slide += (background.height-slide)/SLIDE_DAMPING;
			if (background.height-slide < 0.5) {
				removeEventListener(Event.ENTER_FRAME, slideOut);
				if (vamoosing) Utils.removeChild(parent, this);
			}
			redrawElements();
		}
		*/
		
		/*
		public function slideIn (e:Event):void {
			//slide += -slide/SLIDE_DAMPING;
			if (slide < 0.5) {
				removeEventListener(Event.ENTER_FRAME, slideIn);
			}
			redrawElements();
		}
		*/
		
		public function playPauseBtnClick (e:Event):void {
			if (disabled) return;
			beacon.dispatchEvent(new VideoEvent(VideoEvent.DO_PLAYPAUSE));
		}
		
		public function muteBtnClick (e:Event):void {
			if (disabled) return;
			beacon.dispatchEvent(new VideoEvent(VideoEvent.DO_TOGGLEMUTE));
		}
		
		public function onVideoMuted (e:Event):void {
			btnSoundOn.selected = false;
		}
		
		public function onVideoUnmuted (e:Event):void {
			btnSoundOn.selected = true;
		}
		
		public function openVolumeSlider (e:Event):void {
			beacon.dispatchEvent(new VideoEvent(VideoEvent.DO_OPEN_VOL_SLIDER));
		}
		
		public function onInfoPosition (e:VideoEvent):void {
			var ratio:Number = Math.min(Math.max(e.position,0),1);
			timecode.htmlText = "<font color='#FFFFFF'>"+Utils.formatTime(ratio*duration)+"</font><font color='#FFFFFF'> / "+Utils.formatTime(duration)+"</font>";
		}
		
		public function fullscreenClick (e:Event):void {
			if (disabled) return;
			beacon.dispatchEvent(new VideoEvent(VideoEvent.DO_GO_FULLSCREEN));
		}
		
		public function onVideoPlaying(e:Event=null):void {
			btnPlay.selected = false;
		}
		
		public function onVideoStopped(e:Event=null):void {
			btnPlay.selected = true;
		}
		
		protected function addTextButton (name:String):TextButton {
			var btn:TextButton = new TextButton(name, 10);
			btn.addEventListener(MouseEvent.CLICK, buttonClicked);
			addChild(btn);
			return btn;
		}
		
		protected function buttonClicked (e:Event):void {
			
			/*
			switch (e.target.text) {
				
				case BTN_RATE_LABEL:
					beacon.dispatchEvent(new VideoEvent(VideoEvent.DO_PANEL_RATE));
					break;
					
				case BTN_EMAIL_LABEL:
					beacon.dispatchEvent(new VideoEvent(VideoEvent.DO_PANEL_EMAIL));
					break;
					
				case BTN_EMBED_LABEL:
					beacon.dispatchEvent(new VideoEvent(VideoEvent.DO_PANEL_EMBED));
					break;
					
				case BTN_INFO_LABEL:
					beacon.dispatchEvent(new VideoEvent(VideoEvent.DO_PANEL_INFO));
					break;
					
				case BTN_DOWNLOAD_LABEL:
					beacon.dispatchEvent(new VideoEvent(VideoEvent.DO_PANEL_DOWNLOAD));
					break;
					
				default:
					trace ("unknown button clicked");
					
			}
			*/
			
		}
		
	}
	
}